package v3;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import util.FileUtils;

import java.io.*;
import java.util.Map;
import java.util.Properties;

public class RunPropertiesUtil {
    private static Properties properties;
    private static String path="config/runProperties.properties";

    static {
        initProperties();
    }

    public static Properties initProperties() {
        if (properties == null) {
            properties = new Properties();
            //如果文件不存在就创建
            File file = new File(path);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        try (InputStream in = new FileInputStream(path);
             InputStreamReader reader = new InputStreamReader(in);) {
            properties.load(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 将Properties对象转换为JSON字符串
     */
    public static String convertPropertiesToJson(Properties properties) {
        // Properties继承自Hashtable，直接转换可能有问题，先转为HashMap
        JSONObject obj = new JSONObject();
        for (String key : properties.stringPropertyNames()) {
            obj.put(key, properties.getProperty(key));
        }
        return obj.toString();
    }

    public static String readByKey(String key) {
        if (properties == null) {
            synchronized (RunPropertiesUtil.class) {
                if (properties == null) {
                    initProperties();
                }
            }
        }
        return properties.getProperty(key);
    }
    public static String readByKey(String key, String defaultValue) {
        if (properties == null) {
            synchronized (RunPropertiesUtil.class) {
                if (properties == null) {
                    initProperties();
                }
            }
        }
        return properties.getProperty(key, defaultValue);
    }


    public static synchronized void updateValue(Map<String,Object> valueMap){
        try(BufferedReader in = new BufferedReader(new FileReader(path));) {
            String line;
            StringBuffer sb = new StringBuffer();
            while ((line = in.readLine()) != null) {
                if(!"".equals(line.trim()) && !line.trim().startsWith("#")){
                    String[] keyV = line.split("=");
                    if(valueMap.containsKey(keyV[0])){
                        line=keyV[0]+"="+valueMap.get(keyV[0]);
                    }
                }
                sb.append(line).append("\r\n");
            }
            if(sb.length() > 2){
                FileUtils.writeFile(path, sb.toString());
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void clear(){
        FileUtils.writeFile(path, "");
    }

    public static void setValue(String key, String value) throws IOException {

        try(BufferedReader in = new BufferedReader(new FileReader(path));) {
            String line;
            StringBuffer sb = new StringBuffer();
            boolean flag = true;
            while ((line = in.readLine()) != null) {
                if(!"".equals(line.trim()) && !line.trim().startsWith("#")){
                    String[] keyV = line.split("=");
                    if(key.equals(keyV[0])){
                        line=key+"="+value;
                        flag = false;
                    }
                }
                sb.append(line).append("\r\n");
            }

            if(flag){
                sb.append(key+"="+value).append("\r\n");
            }

            if(sb.length() > 2){
                FileUtils.writeFile(path, sb.toString());
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        initProperties();
    }

    public static void main(String[] args) {

    }
}
